-
Notifications
You must be signed in to change notification settings - Fork 320
Add enum support
#366
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add enum support
#366
Conversation
068f16a to
0fc277e
Compare
|
One place this does seem to fall down is that you lose the casting when extracting an interface. I can't quite figure out how to do that (possibly something to do with the enum Colour {
White = '000000'
}
const Style = t.type({
colour: t.enum(Colour)
})
interface IStyle extends t.TypeOf<typeof Style> {}
const style: IStyle = {
colour: Colour.White
}
const colour = style.colour // colour has type "any" rather than "Colour"I tried tweaking the signature for const enumType = <E, A = E[keyof E]>(e: E, name: string = 'Enum'): EnumType<A> => {
const is = (u: unknown): u is A => Object.keys(e).some((k) => e[k as keyof E] === u)
return new EnumType<A>(name, is, (u, c) => (is(u) ? success(u) : failure(u, c)), identity)
}...which seems to only succeed in disallowing a |
|
This looks like it might work better, for string enums at least... |
|
Re: [gcanti/io-ts] Add `enum` support (#366)
2020-02-16 2:56 غرينتش-08:00, jessmorecroft <[email protected]>:
… This looks like it might work better, for string enums at least...
```
const enumType = <E>(
e: { [key: string]: E },
name: string = 'Enum',
): EnumType<E> => {
const is = (u: unknown): u is E => Object.keys(e).some(k => e[k] === u);
return new EnumType<E>(
name,
is,
(u, c) => (is(u) ? t.success(u) : t.failure(u, c)),
t.identity,
);
};
```
--
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
#366 (comment)
|
|
Hey, since number enums have remapping, enum E {
A = 1
}
// compiles to
const E {
A: 1,
'1': 'A',
}In this case, I've updated my code here #216 (comment) |
|
Any update on this? |
|
+1 this would be a great addition, as currently I need to manually copy/paste my enum values into a |
c676682 to
9a34f36
Compare
|
Okay I've updated this, and it now correctly casts! Also updated to include @noe132 's update for checking for reverse mapping |
The `enum` is a popular aspect of TypeScript, and can be embedded in
interfaces as a concise, but descriptive, shorthand for literal string
unions:
```typescript
enum Colour {
White = '000000',
Black = 'ffffff'
}
```
This change adds an exported member `enum` to `io-ts`, based on
[this suggestion][1] by @noe132
It means that `enum`s can be reused directly in `io-ts`:
```typescript
const T = t.enum(Colour)
```
[1]: gcanti#216 (comment)
9a34f36 to
a048fbd
Compare
|
Any update on this from @gcanti ? |
|
Hi, what is the current status of this feature? We'd love to have enum support :) |
|
@gcanti How do you feel about merging this in? |
|
Is there anything stopping this feature from being merged? |

Fixes #67
Fixes #216
The
enumis a popular aspect of TypeScript, and can be embedded ininterfaces as a concise, but descriptive, shorthand for literal string
unions:
This change adds an exported member
enumtoio-ts, based onthis suggestion by @noe132
It means that
enums can be reused directly inio-ts: